package com.hero.part.boot.controller;

import org.pac4j.core.config.Config;
import org.pac4j.core.context.J2EContext;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.http.J2ENopHttpActionAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yuez
 * @date 2018/3/5
 */
@Controller
public class CallbackController {

	private CallbackLogic<Object, J2EContext> callbackLogic = new DefaultCallbackLogic<>();

	@Autowired
	private Config config;

	@Value("${pac4j.callback.defaultUrl:#{null}}")
	private String defaultUrl;

	@Value("${pac4j.callback.multiProfile:#{null}}")
	private Boolean multiProfile;

	@Value("${pac4j.callback.renewSession:#{null}}")
	private Boolean renewSession;

	@RequestMapping("/callback")
	public void callback(final HttpServletRequest request, final HttpServletResponse response){
		final J2EContext context = new J2EContext(request,response,config.getSessionStore());

		callbackLogic.perform(context,config,
				J2ENopHttpActionAdapter.INSTANCE,this.defaultUrl,this.multiProfile,this.renewSession);
	}


}
